//
//  YDOAFixedApprovalVC.swift
//  YDOAProduct
//
//  Created by 占益民 on 2017/11/7.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import HandyJSON
import Toaster

/// 复用ID
private let reuseIdType0 = "YDOAFixedApprovalCell"
private let reuseIdType1 = "YDOACarManageCell"
private let reuseIdType2 = "YDOAWorkMonthlyCell"
private let reuseIdType3 = "YDOAWorkMonthlyCell"
private let reuseIdType4 = "YDOAAcuteThingsCell"
private let reuseIdType5 = "YDOAFixedApprovalCell"
private let reuseIdType6 = "YDOAFixedApprovalCell"

private let btnTag = 10000
class YDOAFixedApprovalVC: UIViewController {

 
    @IBOutlet weak var pendingBtn: UIButton!
    @IBOutlet weak var refuseBtn: UIButton!
    @IBOutlet weak var passBtn: UIButton!
    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var mypassTableView: UITableView!
    @IBOutlet weak var myRefuseTableView: UITableView!
    
    var token : String?
    var uid : String?  //用户id
    var type : Int8? // 审批类型
    var page : Int? //page
    
    var currentTabIndex : Int? = 0
    
    lazy var dataSource : Array = Array<HandyJSON>()
    lazy var btnArr : Array<UIButton> = {() -> Array<UIButton> in return []}()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        btnArr = [pendingBtn,refuseBtn,passBtn];
        self.page = 1
        self.navigationController?.navigationBar.isHidden = false
        myTableView.rowHeight = UITableViewAutomaticDimension
        myTableView.estimatedRowHeight = 44;//期望高度
        myTableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            
        })
        myTableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
            
        })
        mypassTableView.rowHeight = UITableViewAutomaticDimension
        mypassTableView.estimatedRowHeight = 44;//期望高度
        myRefuseTableView.rowHeight = UITableViewAutomaticDimension
        myRefuseTableView.estimatedRowHeight = 44;//期望高度

        //根据传过来的type 注册cell
        tableViewRegisterWithType(type: Int(self.type!))
        let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
        self.uid = userinfo["id"] as? String
        self.token = "token"
        self.loadData()
    }

    
    func loadData(){
        var urlStr = ""
        switch self.type{
        case 4?://固定资产审批
            urlStr = String(format: GET_FIX_PROPERTY,self.token!,self.currentTabIndex!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr , params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        guard let model = JSONDeserializer<YDOAFixedPropertyModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
            }) { (error) in
                
            }
            break;
        case 5?://车辆管理审批
            urlStr = String(format: GET_SENDER_CRA,self.token!,self.currentTabIndex!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr , params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        guard let model = JSONDeserializer<YDOASendCarModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
            }) { (error) in
                
            }
            break;
        case 2?://月度需求审批管理
            urlStr = String(format: GET_MONTHLY_DEMAND,self.uid!,self.currentTabIndex!,self.page!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr, params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        guard let model = JSONDeserializer<YDOAMonthlyDemandModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
                
            }) { (error) in
                
            }
            break;
        case 1?://办公用品 领用申请
            urlStr = String(format: GET_OFFICE_GOODS,self.token!,self.currentTabIndex!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr , params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        //与月度需求共用一个model
                        guard let model = JSONDeserializer<YDOAMonthlyDemandModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
            }) { (error) in
                
            }
            break;
        case 3?://急性办公用品 审批
            urlStr = String(format: GET_ACUTE_GOODS,self.token!,self.currentTabIndex!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr , params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        //与月度需求共用一个model
                        guard let model = JSONDeserializer<YDOAAcuteApporvalModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
                
            }) { (error) in
                
            }
            break;
        case 0?://费用报销
            urlStr = String(format: GET_EXPENSE_ACCOUNT,self.token!,self.currentTabIndex!);
            YMLog.log(item: urlStr)
            YMNetworkManager.shareInstance.getRequest(urlString:urlStr , params: nil, success: { (responser) in
                YMLog.log(item: responser);
                if responser["status"]?.int8Value == 1{
                    self.dataSource.removeAll();
                    let dataArr = responser["data"] as! NSArray
                    for dict in dataArr{
                        //与月度需求共用一个model
                        guard let model = JSONDeserializer<YDOAExpenseAccountModel>.deserializeFrom(dict: dict as? NSDictionary) else {
                            return
                        }
                        self.dataSource.append(model)
                    }
                    if self.currentTabIndex == 0{
                        self.myTableView.reloadData()
                    }else if self.currentTabIndex == 2{
                        self.myRefuseTableView.reloadData()
                    }else{
                        self.mypassTableView.reloadData()
                    }
                }
                
            }) { (error) in
                
            }
            break;
        default:
            break;
        }
    }
    
    
    
    func tableViewRegisterWithType(type:Int){
        
        switch type {
        case 4:
            myTableView.register(UINib(nibName: "YDOAFixedApprovalCell", bundle: nil), forCellReuseIdentifier: reuseIdType0)
            myRefuseTableView.register(UINib(nibName: "YDOAFixedApprovalCell", bundle: nil), forCellReuseIdentifier: reuseIdType0)
            mypassTableView.register(UINib(nibName: "YDOAFixedApprovalCell", bundle: nil), forCellReuseIdentifier: reuseIdType0)
            break
        case 5:
            myTableView.register(UINib(nibName: "YDOACarManageCell", bundle: nil), forCellReuseIdentifier: reuseIdType1)
            myRefuseTableView.register(UINib(nibName: "YDOACarManageCell", bundle: nil), forCellReuseIdentifier: reuseIdType1)
            mypassTableView.register(UINib(nibName: "YDOACarManageCell", bundle: nil), forCellReuseIdentifier: reuseIdType1)
            break
        case 2:
            myTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType2)
            myRefuseTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType2)
            mypassTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType2)
            break
        case 1:
            myTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType3)
            myRefuseTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType3)
            mypassTableView.register(UINib(nibName: "YDOAWorkMonthlyCell", bundle: nil), forCellReuseIdentifier: reuseIdType3)
            break
        case 3:
            myTableView.register(UINib(nibName: "YDOAAcuteThingsCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            myRefuseTableView.register(UINib(nibName: "YDOAAcuteThingsCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            mypassTableView.register(UINib(nibName: "YDOAAcuteThingsCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            break
        case 0:
            myTableView.register(UINib(nibName: "YDOACostApplyCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            myRefuseTableView.register(UINib(nibName: "YDOACostApplyCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            mypassTableView.register(UINib(nibName: "YDOACostApplyCell", bundle: nil), forCellReuseIdentifier: reuseIdType4)
            break
       
        default:
            break
        }
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}



//MARK : tabelview  代理方法
extension YDOAFixedApprovalVC : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        switch self.type {
            case 4?:
                return  createFixedCell(tableView: tableView, indexPath: indexPath)
            case 5?:
                return  createCarManageCell(tableView: tableView, indexPath: indexPath)
            case 2?:
                return  createWorkMouthlyCell(tableView: tableView, indexPath: indexPath)
            case 1?:
                return  createWorkThingsCell(tableView: tableView, indexPath: indexPath)
            case 3?:
                return  createAcuteThingsCell(tableView: tableView, indexPath: indexPath)
            case 0?:
                return  createExpenseAccountCell(tableView: tableView, indexPath: indexPath)
            default:
                break
        }
        return UITableViewCell();  
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.type == 4{
            if tableView == myTableView { return 104}
            else if tableView == mypassTableView {return 60}
            else {return 104}
        }else if self.type == 5{
            if tableView == myTableView { return 167}
            else if tableView == mypassTableView {return 120}
            else {return 167}
        }else if self.type == 2{
            if tableView == myTableView { return 146}
            else if tableView == mypassTableView {return 93}
            else {return 146}
        }else if self.type == 1{
            if tableView == myTableView { return 146}
            else if tableView == mypassTableView {return 93}
            else {return 146}
        }else if self.type == 3{
            if tableView == myTableView { return 159}
            else if tableView == mypassTableView {return 107}
            else {return 159}
        }else if self.type == 0{
            if tableView == myTableView { return 172}
            else if tableView == mypassTableView {return 119}
            else {return 172}
        }
        return 100
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

//MARK : 用来解决不同模块进来cell 的创建
extension YDOAFixedApprovalVC {
    
    // type === 0  创建固定资产审批
    func createFixedCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell {
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType0) as? YDOAFixedApprovalCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            weak var weakSelf = self
            let model = self.dataSource[indexPath.row] as! YDOAFixedPropertyModel
            cell.model = model
            
            //固定资产拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUSE_FIXED_PROPERTY, params:params , success: { (responser) in
                        if !responser.isEmpty && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in
                        
                    })
                }
                weakSelf?.present(vc, animated: true) {
                    
                }
            }
            //固定 资产 通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
              let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_FIXED_PROPERTY, params:params , success: { (responser) in
                    if responser != nil && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in
                    
                })
               
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType0) as? YDOAFixedApprovalCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            let model = self.dataSource[indexPath.row] as! YDOAFixedPropertyModel
            cell.model = model
            cell.refuseBtn.isHidden = true
            cell.passBtn.isHidden = true
            cell.lineLab.isHidden = true
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType0) as? YDOAFixedApprovalCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            let model = self.dataSource[indexPath.row] as! YDOAFixedPropertyModel
            cell.model = model
            cell.refuseBtn.isHidden = true
            cell.passBtn.isHidden = true
            cell.lineLab.isHidden = true
            return  cell;
        }
        return UITableViewCell();
    }
   
    
    
    
    //    type === 1 用车申请
    func createCarManageCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell{
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType1) as? YDOACarManageCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            weak var weakSelf = self
            let model = self.dataSource[indexPath.row] as! YDOASendCarModel
            cell.model = model
            
            //固定资产拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUCE_SENDER_CAR, params:params , success: { (responser) in
                        if responser != nil && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in
                        
                    })
                }
                weakSelf?.present(vc, animated: true) {
                    
                }
            }
            //固定 资产 通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_SENDER_CAR, params:params , success: { (responser) in
                    if !responser.isEmpty && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in
                    
                })
                
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType1) as? YDOACarManageCell else {return UITableViewCell();}
            let model = self.dataSource[indexPath.row] as! YDOASendCarModel
            cell.model = model
            cell.selectionStyle = .none
            cell.btnBg.isHidden = true
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType1) as? YDOACarManageCell else {return UITableViewCell();}
            let model = self.dataSource[indexPath.row] as! YDOASendCarModel
            cell.model = model
            cell.selectionStyle = .none
            cell.btnBg.isHidden = false
            return  cell;
        }
        return UITableViewCell();
    }
    
//    type === 2  月度审批
    func createWorkMouthlyCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell{
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType2) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            weak var weakSelf = self
            //拒绝点击
            // 月度需求 拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                weakSelf?.present(vc, animated: true) {
                    
                }
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUSE_MONTHLY_DEMAND, params:params , success: { (responser) in
                        if responser != nil && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in
                        
                    })
                }
               
            }
            // 月度需求  通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_MONTHLY_DEMAND, params:params , success: { (responser) in
                    if responser != nil && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in
                    
                })
                
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType2) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            cell.selectionStyle = .none
            cell.btnBg.isHidden = true
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType2) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            cell.selectionStyle = .none
            return  cell;
        }
        return UITableViewCell();
    }
    
    //    type === 3  办公用品领用审批
    func createWorkThingsCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell{
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType3) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            weak var weakSelf = self
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            //拒绝点击
            // 办公用品领用 拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                weakSelf?.present(vc, animated: true) {
                    
                }
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUSE_OFFICE_GOODS, params:params , success: { (responser) in
                        if !responser.isEmpty && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in
                        
                    })
                }
                
            }
            // 办公用品领用  通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_OFFICE_GOODS, params:params , success: { (responser) in
                    if !responser.isEmpty && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in
                    
                })
                
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType3) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            cell.selectionStyle = .none
            cell.btnBg.isHidden = true
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType3) as? YDOAWorkMonthlyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            let model = self.dataSource[indexPath.row] as! YDOAMonthlyDemandModel
            cell.model = model
            return  cell;
        }
        return UITableViewCell();
    }
    
    
    //    type === 4  急性物品审批
    func createAcuteThingsCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell{
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOAAcuteThingsCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            weak var weakSelf = self
            let model = self.dataSource[indexPath.row] as! YDOAAcuteApporvalModel
            cell.model = model as! YDOAAcuteApporvalModel
            //拒绝点击
            // 办公用品领用 拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                weakSelf?.present(vc, animated: true) {
                    
                }
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUSE_ACUTE_GOODS, params:params , success: { (responser) in
                        if !responser.isEmpty && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in

                    })
                }
                
            }
            // 办公用品领用  通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_ACUTE_GOODS, params:params , success: { (responser) in
                    if !responser.isEmpty && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in

                })
                
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOAAcuteThingsCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            cell.btnBg.isHidden = true
            let model = self.dataSource[indexPath.row] as! YDOAAcuteApporvalModel
            cell.model = model as! YDOAAcuteApporvalModel
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOAAcuteThingsCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            cell.btnBg.isHidden = false
            let model = self.dataSource[indexPath.row] as! YDOAAcuteApporvalModel
            cell.model = model
            return  cell;
        }
        return UITableViewCell();
    }
    
    
 //    type ===6  报销申请 createExpenseAccountCell
    func createExpenseAccountCell(tableView:UITableView, indexPath:IndexPath ) -> UITableViewCell{
        if tableView == myTableView {
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOACostApplyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            weak var weakSelf = self
            let model = self.dataSource[indexPath.row] as! YDOAExpenseAccountModel
            cell.model = model as! YDOAExpenseAccountModel
            //拒绝点击
            // 费用报销 拒绝回调
            cell.refuseCallBack = {(id : Int) in
                let  vc = YDOARefuceResonVCViewController()
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                weakSelf?.present(vc, animated: true) {
                    
                }
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                vc.callBack = {(refuseStr : String) in
                    let params = [
                        "token":userinfo["token"] ?? "",
                        "uid":userinfo["id"] ?? "",
                        "refusereson":refuseStr,
                        "id":String(id)
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: REFUSE_EXPENSE_ACCOUNT, params:params , success: { (responser) in
                        if responser != nil && responser["status"]?.int8Value == 1{
                            Toast(text: "拒绝成功", delay: 0, duration: 1.5).show()
                            self.loadData();
                        }
                    }, failture: { (error) in
                        
                    })
                }
                
            }
            // 费用报销通过  通过回调
            cell.passCallBack = {(id : Int) in
                YMLog.log(item: id)
                let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                let params = [
                    "token":userinfo["token"] ?? "",
                    "uid":userinfo["id"] ?? "",
                    "id":String(id)
                    ] as [String : Any]
                YMNetworkManager().postRequset(urlString: PASS_EXPENSE_ACCOUNT, params:params , success: { (responser) in
                    if responser != nil && responser["status"]?.int8Value == 1{
                        Toast(text: "通过成功", delay: 0, duration: 1.5).show()
                        self.loadData();
                    }
                }, failture: { (error) in
                    
                })
                
            }
            return  cell;
        }else if tableView == mypassTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOACostApplyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            cell.btnBg.isHidden = true
            let model = self.dataSource[indexPath.row] as! YDOAExpenseAccountModel
            cell.model = model as! YDOAExpenseAccountModel
            return  cell;
        }else if  tableView == myRefuseTableView{
            guard let cell =   tableView.dequeueReusableCell(withIdentifier: reuseIdType4) as? YDOACostApplyCell else {return UITableViewCell();}
            cell.selectionStyle = .none
            cell.btnBg.isHidden = false
            let model = self.dataSource[indexPath.row] as! YDOAExpenseAccountModel
            cell.model = model as! YDOAExpenseAccountModel
            return  cell;
        }
        return UITableViewCell();
    }
}



//MARK: 按钮点击方法
extension YDOAFixedApprovalVC {
    
    @IBAction func pendingBtnClick(_ sender: UIButton) {
        YMLog.log(item: "待审批点击")
        self.currentTabIndex = 0
        self.loadData()
        setBtnStatus(button:sender)
    }
    
    @IBAction func refuseBtnClick(_ sender: UIButton) {
         YMLog.log(item: "拒绝点击")
         self.currentTabIndex = 2
         self.loadData()
         setBtnStatus(button:sender)
    }
    @IBAction func passBtnClick(_ sender: UIButton) {
        YMLog.log(item: "通过点击")
        self.currentTabIndex = 1
        self.loadData()
        setBtnStatus(button:sender)
    }
    
    func setBtnStatus(button:UIButton){
        
        let tableArr = [myTableView,mypassTableView,myRefuseTableView]
        for btn in btnArr {
            btn.isSelected = false
        }
        button.isSelected = true
        for tableview in tableArr {
            tableview?.isHidden = true;
        }
        tableArr[button.tag - btnTag - 1]?.isHidden = false;
    }
}



